<template>
  <el-dialog :close-on-click-modal="false"   title="导入" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="工牌文件">
        <el-upload
          :action="action"
          :limit="1"
          :data="form"
          :before-upload="handleBeforeUpload"
          :on-error="handleUploadError"
          :on-success="handleUploadSuccess"
          :show-file-list="false"
          :accept="fileType.join(',')"
          :headers="headers"
          ref="fileUpload"
        >
          <!-- 上传按钮 -->
          <el-button icon="el-icon-upload">导入工牌</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="close">关 闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getToken } from '@/utils/auth';
export default {
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    show: {
      handler(val) {
        if (val) {
          this.resetForm('form');
          this.$refs.fileUpload && this.$refs.fileUpload.clearFiles();
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      action: window.$setting.baseUrl + '/attendance-saas/card/import',
      headers: { appKey: window.$setting.appKey, Authorization: getToken() },
      token: getToken(),
      fileType: ['xlsx'],
      fileSize: '5',
      form: {},
      rules: {
        contractId: [{ required: true, message: '请输入内容', trigger: ['change', 'input'] }]
      }
    };
  },
  methods: {
    // 上传前校检格式和大小
    handleBeforeUpload(file) {
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join('/')}格式文件!`);
          return false;
        }
      }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      // 校检选择
      if (!this.form.contractId) {
        this.$modal.msgError('请先选择合同');
        return false;
      }
      this.$modal.loading('正在上传文件，请稍候...');
      return true;
    },
    handleUploadSuccess(res, file) {
      this.$refs.fileUpload.clearFiles();
      this.$modal.closeLoading();
      if (res.code === 1) {
        this.$modal.msgSuccess('导入成功');
      } else {
        this.$modal.msgError('导入失败');
      }
      // if (res.code === 1) {
      //   if (res.data.flag) {
      //     this.$modal.msgSuccess('导入成功');
      //   } else {
      //     this.$modal
      //       .confirm(res.data.msg + '确定下载失败数据？')
      //       .then((res) => {
      //         this.download('/urban/card-info/getData', {}, `工牌导入失败数据_${new Date().getTime()}.xlsx`);
      //       })
      //       .catch();
      //   }
      // } else {
      //   this.$modal.msgError('导入失败');
      // }
    },
    handleUploadError(err) {
      this.$refs.fileUpload.clearFiles();
      this.$modal.msgError('上传文件失败，请重试');
      this.$modal.closeLoading();
    },
    close() {
      this.form = {};
      this.$emit('update:show', false);
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
}
</style>
